package com.xfcy.blog.common;

import com.xfcy.blog.common.enums.ResultCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 晓风残月Lx
 * @date 2023/4/9 9:38
 */
@ApiModel(value = "统一返回结果类")
@Data
public class R implements Serializable {
    /**
     * 响应码
     */
    @ApiModelProperty(value = "响应码", required = true)
    private Integer code;

    /**
     * 消息内容
     */
    @ApiModelProperty(value = "响应信息", required = false)
    private String msg;

    /**
     * 响应中的数据
     */
    @ApiModelProperty(value = "响应数据", required = false)
    private Object data;

    @ApiModelProperty(value = "响应数据", required = false)
    private Map<String, Object> extra = new HashMap<String, Object>();

    public R(Integer code, String message, Object data) {
        this.code = code;
        this.msg = message;
        this.data = data;
    }


    public R add(String key, Object value) {
        this.extra.put(key, value);
        return this;
    }

    public static R error(String message) {
        return new R(ResultCode.FAILURE.getCode(), message, null);
    }

    public static R error() {
        return new R(ResultCode.FAILURE.getCode(), ResultCode.FAILURE.getDesc(), null);
    }

    public static R error(Integer code, String message) {
        return new R(code, message, null);
    }

    public static R success() {
        return new R(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getDesc(), null);
    }

    public static R success(String message, Object data) {
        return new R(ResultCode.SUCCESS.getCode(), message, data);
    }

    public static R success(Object data) {
        return new R(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getDesc(), data);
    }

    public static R success(Integer code, String message, Object data) {
        return new R(code, message, data);
    }

    public static R success(Integer code, String message) {
        return new R(code, message, null);
    }


}
